package com.kay;

import com.kay.dao.OrderMapper;
import com.kay.dao.UserMapper;
import com.kay.pojo.Order;
import com.kay.pojo.Role;
import com.kay.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Description: Mybatis 基础使用Demo 基本增删改查，一对多，多对多，一对一查询
 * @Author: Kay
 * @CreateDate: 2021/6/27$ 10:32$
 * @UpdateUser: Kay
 * @UpdateDate: 2021/6/27$ 10:32$
 */
public class MyTest {
    @Test
    public void testInsert() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        userMapper.insertOne(new User(3, "kay", "234", "2020-01-01"));
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void testUpdate() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession(true);
        // openSession 创建SqlSession对象默认开启事务，但是该事务不会自动提交 openSession（true）mybatis会提交事务
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        user.setId(1);
        user.setUsername("kaykay");
        userMapper.updateUsername(user);
        sqlSession.close();
    }

    @Test
    public void testDelete() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession(true);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        userMapper.deleteById(3);
        sqlSession.close();
    }

    @Test
    public void testSelect() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = new User();
//        user.setUsername("kay");
        user.setId(1);
        List<User> userList = userMapper.findByCondition(user);
        sqlSession.close();
        if (userList != null) {
            for (User user1 : userList) {
                System.out.println(user1);
            }
        }
    }

    /**
     * 多表一对一查询
     * 查找所有订单及对应的用户
     */
    @Test
    public void testMultiTableOneToOne() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        List<Order> orderList = orderMapper.findAllOrderUser();
        if (orderList != null){
            for (Order order : orderList) {
                System.out.println(order);
                System.out.println("user : " + order.getUser());
                System.out.println("________________________________");
            }
        }
    }

    /**
     * 多表 一对多查询
     * 查资多有用户及订单
     */
    @Test
    public void testMultiTableOneToMore() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userOrderList = userMapper.findAllUserOrderList();
        if (userOrderList != null){
            for (User user : userOrderList) {
                System.out.println("user : " + user);
                List<Order> orderList = user.getOrderList();
                if (orderList != null){
                    System.out.println(" order list : ");
                    for (int i = 0; i < orderList.size(); i++) {
                        System.out.println(" order" + (i+1) + ": " + orderList.get(i) );
                    }
                }
                System.out.println("-----------------------------------------");
            }
        }
    }

    /**
     * 多表 多对多查询
     * 查询所有用户及其角色
     */
    @Test
    public void testMultiTableMoreToMore() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userRoleList = userMapper.findAllUserRoleList();
        if (userRoleList != null){
            for (User user : userRoleList) {
                System.out.println("user : " + user);
                List<Role> roleList = user.getRoleList();
                if (roleList != null){
                    System.out.println(" role list : ");
                    for (int i = 0; i < roleList.size(); i++) {
                        System.out.println(" role" + (i+1) + ": " + roleList.get(i) );
                    }
                }
                System.out.println("-----------------------------------------");
            }
        }
    }

    /**
     * forEach语法测试
     * 查询所有id在数组下的用户及其角色
     * @throws IOException
     */
    @Test
    public void testForeachSql() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int[] ids = {1,2};
        List<User> userRoleList = userMapper.findUserRoleListByIds(ids);
        if (userRoleList != null){
            for (User user : userRoleList) {
                System.out.println("user : " + user);
                List<Role> roleList = user.getRoleList();
                if (roleList != null){
                    System.out.println(" role list : ");
                    for (int i = 0; i < roleList.size(); i++) {
                        System.out.println(" role" + (i+1) + ": " + roleList.get(i) );
                    }
                }
                System.out.println("-----------------------------------------");
            }
        }
    }
}
